#!/usr/bin/python

import MySQLdb,urllib2, tagpot
import re
from xml.dom import minidom

#db setting
cursor = tagpot.dbConnect()


#
# allblog searching
#
def readURL(url):
	request = urllib2.Request(url)
	opener = urllib2.build_opener()
	feed = opener.open(request).read()
	xml = minidom.parseString(feed)
	items = xml.getElementsByTagName("item")
	return items

d = readURL("http://rss.allblog.net/AllPosts.xml")

urlFromAllblog = []
for i in d:
	source = i.getElementsByTagName("source")
	power = source[0].attributes["url"].value
	urlFromAllblog.append(power)
	print power

#domain specific setting
p_naver = re.compile('http://blog.naver.com/([^/]*)(/.*|)')
#p_daum = re.compile('blog.daum.net/([^/]*)(/.*|)')
p_tistory = re.compile('http://([^.].*).tistory.com(/.*|)')
p_egloos = re.compile('http://([^.].*).egloos.com(/.*|)')
#f = open("list/contacts")
#out = open("out.txt", "w")
#ok = f.readlines()

for url in urlFromAllblog:
        """if p_naver.search(url):
                bURL = p_naver.sub(r'blog.naver.com/\1',url)
                #out.write(bURL)
                fURL= bURL
                print bURL
        elif p_daum.search(url):
                bURL = p_daum.sub(r'blog.daum.net/\1',url)
                #out.write(bURL)
                print bURL
        el"""
        
	if p_tistory.search(url):
	    bURL = p_tistory.sub(r'http://\1.tistory.com',url)
	    bURL = bURL.strip()
	    c = 1
	    
	    # duplicated data check
	    cursor.execute("""select url from cts where url=%s""",bURL)
	    ok = cursor.fetchall()
	    for r in ok:
			print "\tduplicated: "+r[0]
			c = 0
		
        if c :
			cursor.execute("""insert into cts(url, xml, hit) 
						 values(%s, %s, 0)""",
				(bURL,bURL+'/rss')
			)
			print "inserted: "+bURL
	elif p_naver.search(url):
		bURL = p_naver.sub(r'http://blog.naver.com/\1',url)
		rss = p_naver.sub(r'http://blog.rss.naver.com/\1.xml',url)
		bURL = bURL.strip()
		c = 1

		# duplicated data check
		cursor.execute("""select url from cts where url=%s""",bURL) 
		ok = cursor.fetchall()
		for r in ok:
			print "\tduplicated: "+r[0]
			c = 0
			
		if c :
			cursor.execute("""insert into cts(url, xml, hit) 
						 values(%s, %s, 0)""",
				(bURL,rss)
			)
			print "inserted: "+bURL